﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LightWeightORM.ORM.TableAccessor;
using SmartWarehouse.Common;
using SmartWarehouse.ORM.Entity;
using SmartWarehouse.Config;
using System.Globalization;


namespace SmartWarehouse
{
    namespace BussinessLogic
    {
        namespace Basis
        {
            /// <summary>
            /// Summary description for TransactionFacade
            /// </summary>
            public class TransactionFacade
            {
                private TableAccessor<SwTransaction> _transAccessor;

                public TableAccessor<SwTransaction> TransAccessor
                {
                    get { return _transAccessor; }
                    set { _transAccessor = value; }
                }

                public TransactionFacade()
                {
                    this.TransAccessor = AccessorFactory.TransactionAcessor;
                }

                public SwMessage AddTransaction(SwTransaction trans)
                {
                    SwMessage message = new SwMessage(SwMessageType.Ok, "已成功出库");
                    if (trans.Id == null || trans.Id == string.Empty)
                        trans.Id = IdGenerator.New();

                    ProductFacade productFacade = new ProductFacade();
                    int nProductCount = productFacade.ProductAccessor.RecordCount(new Filter(SwProduct._Name, trans.Product_name, ComparisonOperator.Equal));

                    if (nProductCount == 0)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = "该产品没有维护";
                        return message;
                    }
                    else
                    {
                        this.TransAccessor.Insert(trans);
                    }

                    return message;
                }


                public DataTable GetAddInvertoryList(string productName, string startDate, string endDate)
                {
                    DataTable addInvList = new DataTable();

                    List<Filter> filters = new List<Filter>();
                    filters.Add(new Filter(SwGoodReceive._Product_name, productName, ComparisonOperator.Like));

                    if (startDate != string.Empty)
                    {
                        filters.Add(new Filter(SwGoodReceive._Datetime, DateTime.Parse(startDate, new CultureInfo("en-US", true)),"startDate", ComparisonOperator.GreaterOrEqual));
                    }
                    if(endDate != string.Empty)
                    {
                        filters.Add(new Filter(SwGoodReceive._Datetime, DateTime.Parse(endDate, new CultureInfo("en-US", true)),"endDate" ,ComparisonOperator.LessOrEqual));
                    }

                    return AccessorFactory.GoodReceiveAccessor.ToDataTable(filters.ToArray());
                }
                public DataTable GetShipmentList(string productName, string startDate, string endDate)
                {
                    DataTable addInvList = new DataTable();

                    List<Filter> filters = new List<Filter>();
                    filters.Add(new Filter(SwShipment._Product_name, productName, ComparisonOperator.Like));

                    if (startDate != string.Empty)
                    {
                        filters.Add(new Filter(SwGoodReceive._Datetime, DateTime.Parse(startDate, new CultureInfo("en-US", true)), "startDate", ComparisonOperator.GreaterOrEqual));
                    }
                    if (endDate != string.Empty)
                    {
                        filters.Add(new Filter(SwGoodReceive._Datetime, DateTime.Parse(startDate, new CultureInfo("en-US", true)), "startDate", ComparisonOperator.GreaterOrEqual));
                    }

                    return AccessorFactory.ShipmentAccessor.ToDataTable(filters.ToArray());
                }
            }
        }
    }
}